<?php

namespace App\V3\V3_2_7\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AdminModel extends Model
{


    use HasFactory;

    protected $table = 'admin';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    public static function getAccountInfo(int $userId)
    {
        return self::where('id', $userId)->first();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //获取审批人列表
    static public function getApproverList($parentId)
    {
        return self::select(
            'id',
            'username',
            'nickname',
            'mobile',
            'parent_id'
        )
            ->where('parent_id', $parentId)
            ->where('delete_flag', 0)
            ->where('is_active', 1)
            ->orWhere('id', $parentId)
            ->orderBy('id', 'ASC')
            ->get();
    }

    //用户列表
    static public function userList($parentId, $data)
    {
        $result = self::select(
            'id',
            'username',
            'nickname',
            'mobile',
            'is_active',
            'newpwd',
            'position',
            'created_at',
            'updated_at'
        )
            ->where([
                'parent_id' => $parentId,
                'delete_flag' => 0
            ])
            ->where(function($query) use ($data) {
                $query->orWhere('nickname', 'like', '%'.$data['keyword'].'%');
                $query->orWhere('username', 'like', '%'.$data['keyword'].'%');
            })
            ->orderBy('id', 'DESC')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
        if(!empty($result['data'])){
            $storageList = StorageModel::where([
                'main_id' => $parentId,
                'delete_flag' =>0
            ])->get();

            if(!empty($storageList->toArray())){
                $storageList = $storageList->toArray();
                foreach ($result['data'] as &$uVal){
                    $storageNameArr = [];
                    foreach ($storageList as $sVal){
                        if($sVal['user_id'] == $uVal['id']){
                            $storageNameArr[] = $sVal['storage_name'];
                        }else{
                            if(!empty($sVal['multi_user_id'])){
                                if(strpos($sVal['multi_user_id'],',')!=false){
                                   $multiUserIdArr = explode(',',$sVal['multi_user_id']);
                                   if(in_array($uVal['id'],$multiUserIdArr)){
                                       $storageNameArr[] = $sVal['storage_name'];
                                   }
                                }elseif($sVal['multi_user_id'] == $uVal['id']){
                                    $storageNameArr[] = $sVal['storage_name'];
                                }
                            }
                        }
                    }
                    $uVal['storage_name']  = implode(',',$storageNameArr);
                }
            }
        }

        return $result;
    }

    //变更手机号
    public static function changeMobile($userId, $data)
    {
        return self::where('id', $userId)->update([
            'mobile' => $data['mobile'],
            'username' => $data['mobile'],
        ]);
    }

    //选择用户列表
    static public function selUserList($data)
    {
        return self::select(
            'id',
            'username',
            'nickname',
            'mobile',
            'is_active',
            'position'
            )->where([
                'parent_id' => $data['main_id'],
                'delete_flag' => 0
            ])->orderBy('id', 'DESC')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
    }
}
